#include "main.h"
#include "messager.h"

// ##
#define LOG_TAG     "boot"
#include "log.h"

static const char *logo_text[] = {
    "\r\n"
    " __",
    "/ _|",
    "| |_ _ __ ___  _____      _____  __ _ _ __",
    "|  _| '__/ _ \\/ _ \\ \\ /\\ / / _ \\/ _` | '__|",
    "| | | | |  __/  __/\\ V  V /  __/ (_| | |",
    "|_| |_|  \\___|\\___| \\_/\\_/ \\___|\\__,_|_|"
    "\r\n"
};

// ##
void thread_boot(void const *pvParameter)
{
    log_init();

    // ##
#ifdef CFG_SUPPORT_RTC
    rtc_init();
#endif

#ifdef CFG_PLATFORM_BACKTRACE
    // ##
    cm_backtrace_init(DEVICE_NAME, HARDWARE_VERSION, SOFTWARE_VERSION);
#endif

    osVersion_t version;
    char kernal[128];
    osKernelGetInfo(&version, kernal, sizeof(version));

    // ##
    int i;
    for (i = 0; i < sizeof(logo_text) / sizeof(logo_text[0]); i++)
    {
        mr_printf("%s\n", logo_text[i]);
    }
    log_info("Device name : %s.\r\n", DEVICE_NAME);
    log_info("Product model : %s.\r\n", DEVICE_MODEL);
    log_info("Hardware version : %s.\r\n", HARDWARE_VERSION);
    log_info("Firmware version : %s.\r\n", SOFTWARE_VERSION);
    log_info("Designed by %s.\r\n", MANUFACTURER_NAME);
    log_info("Kernel ID: %s\n", kernal);
    log_info("Kernel version: %d.%d.%d\n", version.api, version.kernel);
    log_info("Built at %s %s.\r\n", __TIME__, __DATE__);

    // ##
    messager_init();

    // ##
    osThreadDef(thread_notify, "thread_notify", THREAD_NOTIFY_PRIORITY, 0, THREAD_NOTIFY_STACK_DEPTH);
    thread_notify_id = osThreadCreate(osThread(thread_notify), NULL);

    // ##
    osThreadDef(thread_display, "thread_display", THREAD_DISPLAY_PRIORITY, 0, THREAD_DISPLAY_STACK_DEPTH);
    thread_display_id = osThreadCreate(osThread(thread_display), NULL);

    // ##
#ifndef SIMULATOR
    osThreadTerminate(osThreadGetId());
#else
    while (1)
    {
        osDelay(1000);
        osSignalSet(thread_notify_id, SIGNAL_SYNC_BIT_RTC);
    }
#endif
}

